package cn.gmcx.lazy.izze.service.impl;

import cn.gmcx.lazy.izze.config.AliyunConfig;
import cn.gmcx.lazy.izze.exception.BizException;
import cn.gmcx.lazy.izze.service.FileService;
import cn.gmcx.lazy.izze.utils.OssGetObjectNameUtils;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URI;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Transactional
    @Override
    public String getVideo_url(MultipartFile file) {
        String oldFilename = file.getResource().getFilename();
        String substring = oldFilename.substring(oldFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString().replace("-","");
        String finalFilename = "video/"+newFilename +substring;
        try{
            //核心代码---上传文件到阿里云OSS
            PutObjectResult result = AliyunConfig.oSSClient().putObject(AliyunConfig.bucketName,finalFilename,file.getInputStream());
            if (result != null){
                return "https://"+AliyunConfig.bucketName+"."+AliyunConfig.endpoint+"/"+finalFilename;//返回给controller的文件路径（用来保存到数据库）
            }
        }catch (Exception e){
            e.printStackTrace();
            return "出错！";
        }finally {
            AliyunConfig.oSSClient().shutdown();//关闭oss服务
        }
        return null;
    }
    @Transactional
    @Override
    public String getImg_url(MultipartFile file) {
        String oldFilename = file.getResource().getFilename();
        String substring = oldFilename.substring(oldFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString().replace("-","");
        String finalFilename = "avatar/"+newFilename +substring;
        try{
            //核心代码---上传文件到阿里云OSS
            PutObjectResult result = AliyunConfig.oSSClient().putObject(AliyunConfig.bucketName,finalFilename,file.getInputStream());
            if (result != null){
                return "https://"+AliyunConfig.bucketName+"."+AliyunConfig.endpoint+"/"+finalFilename;//返回给controller的文件路径（用来保存到数据库）
            }
        }catch (Exception e){
            e.printStackTrace();
            return "出错！";
        }finally {
            AliyunConfig.oSSClient().shutdown();//关闭oss服务
        }
        return null;
    }
    @Transactional
    @Override
    public Integer remove_file(String fileName) {
        String objectName = OssGetObjectNameUtils.getObjectName(fileName);
        try{

            AliyunConfig.oSSClient().deleteObject("lzze",objectName);
            return 200;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 500;
    }

    @Override
    public String getVideo_url2(MultipartFile file) {
        String oldFilename = file.getResource().getFilename();
        String substring = oldFilename.substring(oldFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString().replace("-","");

        String uploadPath = "C:\\Users\\琪琪\\Desktop\\IZEEfiles\\video";
        String finalFilename =newFilename +substring;

        File directory = new File(uploadPath);

        if (!directory.exists()){
            directory.mkdirs();
        }

        File destinationFile = new File(directory,finalFilename);

        try {
            file.transferTo(destinationFile);
            return "http://localhost:8104/video/"+ finalFilename;
        }catch (Exception e){
            e.printStackTrace();
            throw new BizException("上传视频出错");
        }

    }

    @Override
    public String getImg_url2(MultipartFile file) {
        String oldFilename = file.getResource().getFilename();
        String substring = oldFilename.substring(oldFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString().replace("-","");

        String uploadPath = "C:\\Users\\琪琪\\Desktop\\IZEEfiles\\avatar";
        String filePath = "avatar";
        return getString(file, substring, newFilename, uploadPath,filePath);

    }

    @Override
    public boolean remove_file2(String fileName) {
        String newFileName = fileName.substring(fileName.lastIndexOf('/'));
        String finalFileName = "C:\\Users\\琪琪\\Desktop\\IZEEfiles\\avatar" + newFileName;
        File file = new File(finalFileName);
        if (file.exists()){
            boolean isDelete = file.delete();
            return isDelete;
        }
        return true;
    }

    @Override
    public String getImg_url_comment(MultipartFile file) {
        String oldFilename = file.getResource().getFilename();
        String substring = oldFilename.substring(oldFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString().replace("-","");

        String uploadPath = "C:\\Users\\琪琪\\Desktop\\IZEEfiles\\comment";
        String filePath = "comment";
        return getString(file, substring, newFilename, uploadPath,filePath);

    }

    private String getString(MultipartFile file, String substring, String newFilename, String uploadPath,String filePath) {
        String finalFilename =newFilename +substring;

        File directory = new File(uploadPath);

        if (!directory.exists()){
            directory.mkdirs();
        }

        File destinationFile = new File(directory,finalFilename);

        try {
            file.transferTo(destinationFile);
            return "http://localhost:8104/"+filePath+"/"+finalFilename;
        }catch (Exception e){
            e.printStackTrace();
            throw new BizException("上传头像出错");
        }
    }

}
